package com.tools.db.ip;

import com.tools.common.object.Note;
import com.tools.common.security.SysKit;
import org.lionsoul.ip2region.xdb.Searcher;

import java.util.List;
import java.util.Map;

/**
 * 支持并发的 IP 搜索工具实例
 * */
@Note("支持并发的 IP 搜索工具实例")
public final class IPSearcher {

    @Note("IP 信息搜索实例")
    private final Searcher searcher;


    public IPSearcher(String dbFile) {
        this.searcher = IPKit.loadAllDataSearcher(dbFile);
    }

    public Searcher getSearcher() {
        return searcher;
    }

    /* ********************************************************************************************
     *
     *          功能方法
     *
     * *********************************************************************************************
     * */

    public long toNumberIP(String ip) {
        return SysKit.toNumberIP(ip);
    }

    public String toStringIP(int ip) {
        return SysKit.toStringIp(ip);
    }

    @Note("获取 IP 归属信息的字符串，格式为 【国家|区域|省份|城市|运营商】")
    public String findToString(String ip) {
        return IPKit.findIPToString(this.searcher, ip);
    }

    public String findToString(int ip) {
        return IPKit.findIPToString(this.searcher, ip);
    }

    public IPBean findToBean(String ip) {
        return IPKit.findIPToBean(this.searcher, ip);
    }

    public IPBean findToBean(int ip) {
        return IPKit.findIPToBean(this.searcher, ip);
    }

    public List<IPBean> findMoreAsList(String... ips) {
        return IPKit.findMoreAsList(this.searcher, ips);
    }

    public Map<String, IPBean> findMoreAsMap(String... ips) {
        return IPKit.findMoreAsMap(this.searcher, ips);
    }

    public Map<String, IPBean> findMoreAsMapByStringIP(Iterable<String> ips) {
        return IPKit.findMoreAsMapByStringIP(this.searcher, ips);
    }

    public List<IPBean> findMoreAsList(int... ips) {
        return IPKit.findMoreAsList(this.searcher, ips);
    }

    public Map<Integer, IPBean> findMoreAsMap(int... ips) {
        return IPKit.findMoreAsMap(this.searcher, ips);
    }

    public Map<Integer, IPBean> findMoreAsMapByNumberIP(Iterable<Integer> ips) {
        return IPKit.findMoreAsMapByNumberIP(this.searcher, ips);
    }

    public void close() {
        IPKit.close(this.searcher);
    }
}
